package com.jcfk.system.controller;

import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.*;
import com.jcfk.system.domain.dto.SysFrequentMenuDTO;
import com.jcfk.system.domain.query.SysFrequentMenuQuery;
import com.jcfk.system.service.SysFrequentMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 常用菜单表
 *
 * @author yrl
 * @email 
 * @date 2025-06-03
 */
@Api(tags = {"常用菜单表管理"})
@RestController
@RequestMapping("sysfrequentmenu")
public class SysFrequentMenuController {
    @Autowired
    private SysFrequentMenuService sysFrequentMenuService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysFrequentMenuQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询常用菜单表")
    @PostMapping("/queryByPage")
    public RetPage<SysFrequentMenuDTO> queryByPage(@RequestBody ReqPageParam<SysFrequentMenuQuery> requestParam) {

        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<SysFrequentMenuQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<SysFrequentMenuDTO> page = new DataPage<SysFrequentMenuDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            SysFrequentMenuDTO.class);
        requestParam.getData().setUserId(SecurityUtils.getUserId());
        page = sysFrequentMenuService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取菜单树  用于角色分权权限的时候使用
     *
     * @return
     */
    @ApiOperation(value = "获取菜单树  用于角色分权权限的时候使用", notes = "获取菜单树  用于角色分权权限的时候使用", httpMethod = "POST")
    @PostMapping("tree")
    public RetResult<List<SysFrequentMenuDTO>> tree(String permissionType) {
        return RetResult.ok(sysFrequentMenuService.tree(permissionType));
    }


    /**
     * 获取登录人的常用菜单
     *
     * @return
     */
    @ApiOperation(value = "获取登录人的常用菜单", notes = "获取菜单树  用于角色分权权限的时候使用", httpMethod = "POST")
    @PostMapping("getOwnList")
    public RetResult<List<SysFrequentMenuDTO>> getOwnList(String permissionType) {
        return RetResult.ok(sysFrequentMenuService.getOwnList(permissionType));
    }


    /**
     *  常用菜单表
     * 根据id 查询详情
     */
    @ApiOperation("查询常用菜单表")
    @GetMapping("/get/{id}")
    public RetResult<SysFrequentMenuDTO> get(@PathVariable("id") String id) {
            SysFrequentMenuDTO item = sysFrequentMenuService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理常用菜单表
     */
    @ApiOperation("保存常用菜单表")
    @PostMapping("/save")
    @RequestLogHeader(modular = "常用菜单表管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid SysFrequentMenuDTO sysFrequentMenu) {
        sysFrequentMenuService.insert(sysFrequentMenu);
        return RetResult.ok();

    }

    /**
     *  批量删除常用菜单表
     */
    @ApiOperation("批量删除常用菜单表")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "常用菜单表管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody SysFrequentMenuDTO sysFrequentMenu) {
        return RetResult.ok(sysFrequentMenuService.logicDeleteList(sysFrequentMenu));
    }





    /**
     *  删除常用菜单表
     */
    @ApiOperation("删除常用菜单表")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "常用菜单表管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        sysFrequentMenuService.deleteItem(id);
        return RetResult.ok();
    }


}
